<%--
  @author Supun Viraj Rathnayaka
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<script src="${pageContext.request.contextPath}/js/printer.js" type="text/javascript"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/jquery.jqplot.css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jquery.jqplot.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jqplot.pieRenderer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jqplot.barRenderer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jqplot.categoryAxisRenderer.js"></script>
<script src="${pageContext.request.contextPath}/js/printer.js" type="text/javascript"></script>


<script type="text/javascript">
    $(function () {
        $('.land-table').html(initTableHead());
        $('#chartdiv').hide();
    });

    function getListingUrl(cr){
        var url = "leaseDetailsList.do?"
        for(var i in cr) {
           switch (parseInt(i)){
               case 0: // branch id
                   url += "branchId="+cr[i];
                   break;
               case 1: // track  id
                   url += "&railwayTrackId="+cr[i];
                   break;
               case 2: // district id
                   url += "&districtId="+cr[i];
                   break;
               case 3: // starting valuation
                   if(isNaN(cr[i])){
                       url += "&startingVal=0";
                   }else{
                       url += "&startingVal="+cr[i];
                   }
                   break;
               case 4: // ending valuation
                   if(isNaN(cr[i])){
                       url += "&endingVal=0";
                   }else{
                       url += "&endingVal="+cr[i];
                   }
                   break;
               case 5: // starting date
                   url += "&startingDate="+cr[i];
                   break;
               case 6: // ending date
                   url += "&endingDate="+cr[i];
                   break;
           }
        }
        return url;

    }

    function loadLandList(landInfo, cr) {
        var opt = '';
        var total = 0;
        opt += initTableHead();
        opt += '<tbody>';
        var list_url = getListingUrl(cr);
        if (landInfo.length > 0) {
            for (var i = 0; i < landInfo.length; i++) {
                opt += '<tr>' +
                        '<td class="center-align">' + getApprovalStateName(landInfo[i][0]) + '</td>' +
                        '<td class="center-align">' + landInfo[i][1] + '</td>' +
                        '<td class="center-align"><a href="'+list_url+'"><img src="${pageContext.request.contextPath}/images/mark-as-complete.png" width="32px" height="32px"/></a></td>' +
                        '</tr>';
                total +=landInfo[i][1];
            }
        }else{
            opt+='<tr>'+
                    '<td colspan="7" class="center-align">' +
                    $('#no-record').val() +
                    '</td>' +
                    '</tr>';
        }

        opt += '</tbody>';
        if(landInfo.length > 1){
            opt+= initTableFoot(total);
        }
        $('.land-table').html(opt);
        if(landInfo.length > 0){
            $('#chartdiv').slideDown("slow",function(){
                showPieChart(landInfo);
            });
        }else{

            $('#chartdiv').slideUp("slow",function(){
                $('#chartdiv').empty();
            });
        }

    }

    function initTableHead(){
        var opt = '';
        opt += '<thead>' +
                '<tr class="table-header">' +
                '<th class="center-align">' + $('#land_status').val() + '</th>' +
                '<th class="center-align">' + $('#no_of_lands').val() + '</th>' +
                '<th class="center-align"> Action </th>' +
                '</tr>' +
               '</thead>';
        return opt;
    }

    function initTableFoot(total){
        var opt = '';
        opt += '<tfoot>' +
                '<tr class="table-header">' +
                '<th class="center-align">' + $('#all_lands_sum').val() + '</th>' +
                '<th class="center-align">' + total + '</th>' +
                '</tr>' +
               '</tfoot>';
        return opt;
    }

    function showPieChart(landInfo){

        var data = new Array();

        for (var i = 0; i < landInfo.length; i++) {
            data.push([getApprovalStateName(landInfo[i][0]),landInfo[i][1]]);
        }
        var plot1 = jQuery.jqplot ('chartdiv', [data],
                {
                    seriesDefaults: {
                        renderer: jQuery.jqplot.PieRenderer,
                        rendererOptions:{
                            showDataLabels: true
                        }
                    },
                    legend: { show:true, location: 'e', fontSize: 20 }
                }
        );
    }

    function getApprovalStateName(name){
        var stateName;
        if(name==="REJECTED"){
            stateName = $('#rejected').val();
        }else if(name==="APPROVED"){
            stateName = $('#approved').val();
        }else if(name==="PENDING"){
            stateName = $('#pending').val();
        }else if(name==="DATA_ENTRY"){
            stateName = $('#data_entry').val();
        }
        return stateName;
    }

    $(function(){

        $('select#railwayTrackId').bind('change', function (evt1) {
            var id = $("select#railwayTrackId").attr("value");
            if(id>0){
                $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id: id, mode: 6},
                        function (data) {
                            var options1 = '<option value="0">' + $('#all').val() + '</option> ';
                            var ds = data.districtList;
                            for (var i = 0; i < ds.length; i++) {
                                options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'
                            }
                            $("select#districtId").html(options1);
                        });
            }else{
                $("select#districtId").html('<option value="0">' + $('#all').val() + '</option> ');
            }
        });

        $('input#submitQuery').click(function (evt1) {
            var crit = [];
            crit.push($("select#branchId").attr("value"));
            crit.push($("select#railwayTrackId").attr("value"));
            crit.push($("select#districtId").attr("value"));
            crit.push($("input#valuationFromId").val());
            crit.push($("input#valuationToId").val());
            crit.push($("input#dateFromId").val());
            crit.push($("input#dateToId").val());
            var type = "LeaseStat";
            $('div#result-table').html('');
            $.getJSON('${pageContext.request.contextPath}/common/RailwayReportService', {type: type, branch: crit[0], track: crit[1], district: crit[2], valFrom: crit[3], valTo: crit[4], dtFrom: crit[5], dtTo: crit[6]},
                    function (data) {
                        loadLandList(data.leaseList,crit);
                    });
        });
    });

</script>

<div id="land-registration-module-home-title" class="module-home-title">
    <s:a href="reportModule.do">
        <span><img src="${pageContext.request.contextPath}/images/print.png" alt="Print">
            <s:label value="%{getText('reports.label')}"/></span>
    </s:a>
    <span class="module-title-breaker"> > </span>
    <s:a href="leaseDetailsReport.do">
        <span><img src="${pageContext.request.contextPath}/images/list.png" alt="">
            <s:label value="Lease Details Report"/></span>
    </s:a>
    <div id="back-button" align="right">
        <s:form><s:submit value="%{getText('back_btn.label')}" action="reportModule"/></s:form>
    </div>
</div>

<div id="printbtn" class="form-button-div right-align">
    <input type="button" style="margin:10" id="printBtnId" value="Print" onclick="printPage('init-land-report-list', 'A4', 'P');"/>
</div>

<div id="init-land-report-list">
    <div class="page-title center-align">
        <h2><s:label value="Lease Statistics"/></h2>
    </div>
    <table class="width-635-px margin-10-auto" id="filter-lists">

        <tr>
            <td colspan="2" class="left-align"><s:label value="Branch"/></td>
        </tr>

        <tr>
            <td colspan="2" class="left-align">
                <select id="branchId">
                    <option value="0">All</option>
                    <option value="1">COM</option>
                    <option value="2">UPP</option>
                    <option value="3">RRM</option>
                </select>
            </td>
        </tr>

        <tr>
            <td><s:label value="Railway Line"/></td>
            <td><s:label value="%{getText('district.label')}"/></td>
        </tr>

        <tr>
            <td class="left-align">
                <s:select id="railwayTrackId" name="trackListId" list="railwayTrackList" headerKey="0"
                          headerValue="%{getText('all.label')}" cssClass="width-250-px"/>
            </td>
            <td class="left-align">
                <select id="districtId">
                    <option value="0">All</option>
                </select>
            </td>
        </tr>

        <tr>
            <td class="left-align">
                <s:label value="Valuation From (Rs)"/>
            </td>
            <td class="left-align">
                <s:label value="Valuation To (Rs)"/>
            </td>
        </tr>

        <tr>
            <td class="left-align">
                <input type="text" id="valuationFromId" onKeyPress="return dateNumbersOnly(event,true)">
            </td>
            <td class="left-align">
                <input type="text" id="valuationToId" onKeyPress="return dateNumbersOnly(event,true)">
            </td>
        </tr>

        <tr>
            <td class="left-align">
                <s:label value="Start Date"/>
            </td>
            <td class="left-align">
                <s:label value="End Date"/>
            </td>
        </tr>

        <tr>
            <td class="left-align">
                <input type="text" id="dateFromId" onclick="selectDate('dateFromId')"
                       onKeyPress="return dateNumbersOnly(event,true)">
            </td>
            <td class="left-align">
                <input type="text" id="dateToId" onclick="selectDate('dateToId')"
                       onKeyPress="return dateNumbersOnly(event,true)">
            </td>
        </tr>

       <%-- <tr>
            <td class="left-align"><s:label value="Alienation State"/></td>
            <td class="left-align">
                <select>
                    <option value="0">--All--</option>
                    <option value="1">Data Entry</option>
                    <option value="2">Branch Officer Approved</option>
                    <option value="3">Section Head Approved</option>
                    <option value="3">Sub Department Head Approved</option>
                    <option value="3">Department Head Approved</option>
                </select>
            </td>
        </tr>--%>

        <tr>
            <td colspan="2" class="right-align margin-top-30px" >
                <s:submit id="submitQuery" value="Submit Query" action="#"></s:submit>
            </td>
        </tr>

        </table>


<table class="width-100 margin-10-auto table land-table">
</table>

<div id="chartdiv" style="height:500px;width:830px; "></div>
</div>
<s:hidden id="display" value="%{getText('display.label')}"/>
<s:hidden id="records" value="%{getText('records.label')}"/>
<s:hidden id="search" value="%{getText('search.label')}"/>
<s:hidden id="no-record" value="%{getText('no_record.label')}"/>
<s:hidden id="showing" value="%{getText('showing.label')}"/>
<s:hidden id="to" value="%{getText('to.label')}"/>
<s:hidden id="of" value="%{getText('of.label')}"/>
<s:hidden id="entries" value="%{getText('entries.label')}"/>
<s:hidden id="first" value="%{getText('first.label')}"/>
<s:hidden id="last" value="%{getText('last.label')}"/>
<s:hidden id="next" value="%{getText('next.label')}"/>
<s:hidden id="previous" value="%{getText('previous.label')}"/>
<s:hidden id="all" value="%{getText('all.label')}"/>
<s:hidden id="rejected" value="%{getText('rejected.label')}"/>
<s:hidden id="approved" value="%{getText('approve.label')}"/>
<s:hidden id="pending" value="%{getText('pending.label')}"/>
<s:hidden id="data_entry" value="%{getText('data_entry.label')}"/>
<s:hidden id="land_status" value="%{getText('land_status.label')}"/>
<s:hidden id="no_of_lands" value="%{getText('no_of_lands.label')}"/>
<s:hidden id="all_lands_sum" value="%{getText('all_lands_sum.label')}"/>